package com.ruitu.mall_98_2;

import android.annotation.TargetApi;
import android.app.DialogFragment;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mine.LoginActivity;

import javax.crypto.Cipher;
@TargetApi(23)
public class FingerprintDialogFragment extends DialogFragment {
    private FingerprintManager fingerprintManager;

    private CancellationSignal mCancellationDignal;

    private Cipher cipher;

    private FingerprintActivity mActivity;

    private TextView errorMsg;
    /**
     *biao'sho标识是否是用户主动取消的认证.
     */
    private boolean isSelfCancelled;

    public void setCipher(Cipher cipher){
        this.cipher = cipher;
    }

    public static FingerprintDialogFragment newInstance() {

        Bundle args = new Bundle();

        FingerprintDialogFragment fragment = new FingerprintDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (FingerprintActivity) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fingerprintManager = getContext().getSystemService(FingerprintManager.class);
        setStyle(DialogFragment.STYLE_NORMAL,android.R.style.Theme_Material_Light_Dialog);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fingerprint_dialog,container,false);
        errorMsg = view.findViewById(R.id.error_msg);
        TextView cancel = view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                stopListening();
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        startListening(cipher);
    }

    @Override
    public void onPause() {
        super.onPause();
        stopListening();
    }

    private void startListening(Cipher cipher) {
        isSelfCancelled = false;
        mCancellationDignal = new CancellationSignal();
        fingerprintManager.authenticate(new FingerprintManager.CryptoObject(cipher), mCancellationDignal, 0, new FingerprintManager.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, CharSequence errString) {
                if (!isSelfCancelled){
                    errorMsg.setText(errString);
                    if (errorCode == FingerprintManager.FINGERPRINT_ERROR_LOCKOUT){
                        ToastUtils.showShort(errString);
                        dismiss();
                    }
                }
            }

            @Override
            public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
                errorMsg.setText(helpString);
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                ToastUtils.showShort("指纹认证成功");
                mActivity.onAuthenticated();
            }

            @Override
            public void onAuthenticationFailed() {
                errorMsg.setText("指纹认证失败,请再试一次");
            }
        },null);
    }

    private void stopListening() {
        if (mCancellationDignal != null){
            mCancellationDignal.cancel();
            mCancellationDignal = null;
            isSelfCancelled = true;
        }
    }
}
